package com.lenovo.test


import java.text.SimpleDateFormat
import java.util.{Calendar, Date}
/**
  * Created by root on 7/29/17.
  */
object last_mag1 {


  def main(args: Array[String]): Unit = {



  println( get_motnth_time_interval("2017-01-21")  )

  }
  def get_motnth_time_interval(date: String): String = {
    if(date ==null || "".equals(date) || "null".equals(date.toLowerCase)) return  ""
    val sdf = new SimpleDateFormat("yyyy-MM-dd")
    val pDate = sdf.parse(date)
    val lDate = getLastMonDay(pDate)
    println(getCurMonDay(pDate))
    println(lDate)
    println(lDate-getCurMonDay(pDate))

    if(getCurMonDay(pDate)<=4 || (lDate-getCurMonDay(pDate)<=4) ) "Month End Closing"
    else if(getCurMonDay(pDate)<4 || (lDate-getCurMonDay(pDate)<=7)) "ROL"
    else if(getCurMonDay(pDate)>4 && getCurMonDay(pDate)<=10 ) "Early Month"
    else if(getCurMonDay(pDate)>10 && getCurMonDay(pDate)<=20 ) "Mid Month"
    else "Late Month"

  }

//  def getAppointDate(date: Date, range: Int): Date = {
//    val lastMonDay = getLastMonDay(date)
//    val ca = Calendar.getInstance
//    ca.setTime(lastMonDay)
//    ca.add(Calendar.DAY_OF_MONTH, range)
//    return ca.getTime
//  }

  def getLastMonDay(date: Date): Int = {
    val ca = Calendar.getInstance
    ca.setTime(date)
    //ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH))
    return ca.getActualMaximum(Calendar.DATE)
  }

  def getCurMonDay(date: Date): Int = {
    val ca = Calendar.getInstance
    ca.setTime(date)
    return ca.get(Calendar.DAY_OF_MONTH)
  }

  import java.util.Calendar

  private def getLastDayOfMonth(year: Int, month: Int) = {
    val cal = Calendar.getInstance
    cal.set(Calendar.YEAR, year)
    cal.set(Calendar.MONTH, month)
    // 某年某月的最后一天
    cal.getActualMaximum(Calendar.DATE)
  }
}
